using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace Tasks
{
	public class SingleTask : IEnumerable
	{
		public SingleTask(IEnumerator enumerator)
		{
			this._enumerator = enumerator;
		}

		//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action onComplete;

		public IEnumerator GetEnumerator()
		{
			if (this._enumerator != null)
			{
				Stack<IEnumerator> stack = new Stack<IEnumerator>();
				IEnumerator task = this._enumerator;
				stack.Push(task);
				while (stack.Count > 0)
				{
					IEnumerator ce = stack.Peek();
					if (!ce.MoveNext())
					{
						stack.Pop();
					}
					else if (ce.Current != ce && ce.Current != null)
					{
						if (ce.Current is IEnumerable)
						{
							stack.Push(((IEnumerable)ce.Current).GetEnumerator());
						}
						else if (ce.Current is IEnumerator)
						{
							stack.Push(ce.Current as IEnumerator);
						}
						else if (ce.Current is WWW)
						{
							stack.Push(new SingleTask.WWWEnumerator(ce.Current as WWW));
						}
					}
					yield return null;
				}
				this._enumerator = null;
			}
			if (this.onComplete != null)
			{
				this.onComplete();
			}
			yield break;
		}

		private IEnumerator _enumerator;

		private class WWWEnumerator : IEnumerator
		{
			public WWWEnumerator(WWW www)
			{
				this._www = www;
			}

			public object Current
			{
				get
				{
					return this._www;
				}
			}

			public bool MoveNext()
			{
				return !this._www.isDone;
			}

			public void Reset()
			{
			}

			private WWW _www;
		}
	}
}
